var config = require('../../../../utils/config');
var API_URL = config.API_URL;
var util = require('../../../../utils/util');
var time, key;
function countdown(that, second, typ) {
    var code_time = that.data.code_time;
    var code_disabled = that.data.code_disabled;
    if (second < 0) {
        code_time = '重新获取';
        code_disabled = false;
        that.setData({
            code_time: code_time,
            code_disabled: code_disabled,
        });
        return;
    }
    time = setTimeout(function () {
        code_time = second--;
        that.setData({
            code_time: code_time
        });
        countdown(that, second);
    }, 1000)
}
Page({
    data: {
        oldCode: {
            disabled: true
        },
        newCode: {
            disabled: true
        },
        template: 'oldCode',
        oldtemplate: 'get',
        code_time: '获取验证码',
        code_disabled: false,
        hidden: true
    },
    onLoad: function (options) {
        let oldCode = this.data.oldCode;
        console.log(oldCode)
        let userInfo = my.getStorageSync({ key: 'user' }).data;
        console.log(userInfo);
        // 这里给oldCode 添加内容
        oldCode.tel = userInfo.phone;
        oldCode.tel_str = util.encrypt(userInfo.phone);
        this.setData({
            oldCode: oldCode
        })
        console.log(oldCode)

    },
    // 输入判断
    oldCodeInput: function (e) {
        let that = this;
        // let oldCode = that.data.oldCode;
        // if (e.detail.value.lenth != 0) {
        //     // 如果输入字数大于0则可以点击
        //     oldCode.disabled = false;
        // } else {
        //     oldCode.disabled = true;
        // }
        // that.setData({
        //     oldCode: oldCode
        // })
        // console.log(oldCode)
    },
    // 解绑发送验证码
    sendOld: function (e) {
        let that = this;
        let oldCode = that.data.oldCode;
        util.PRL(API_URL.sendSms, { mobile: that.data.oldCode.tel, type: 'unbind' })
            .then(res => {
                console.log(res)
                if (res.data.status == 200) {
                    key = res.data.data.key;
                    // 点击获取验证码把  下一步 置为可点击
                    oldCode.disabled = false;
                    that.setData({
                        code_disabled: true,
                        oldCode: oldCode
                    })
                    countdown(that, 120);
                } else {
                    oldCode.disabled = true
                    that.setData({
                        code_disabled: false,
                        oldCode: oldCode
                    })
                    util.showNotice(res.data.msg)
                }
            }).catch(res => {
                console.log(res);
            })
    },
    // 解绑按钮
    oldCode: function (e) {
        console.log(e)
        let that = this;
        let code = e.detail.value.code;
        console.log(code);
        if (code.length == 0) {
            my.showToast({
                content: '请输入验证码',
                duration: 2000
            });
            return
        }
        // 拿到输入的验证码和key 发送给后台验证
        util.PRL(API_URL.checkBindCode, { key: key, code: code })
            .then(res => {
                console.log(res);
                if (res.data.status == 200) {
                    clearTimeout(time);
                    that.setData({
                        template: 'newCode',
                        code_time: '获取验证码',
                        code_disabled: true,
                    })
                }
            }).catch(res => {
                console.log(res);
            })
    },
    // 收不到再次验证
    no_get: function (e) {
        let that = this;
        util.PRL(API_URL.checkBindCode, {})
            .then(res => {
                console.log(res)
                let oldCode = that.data.oldCode;
                oldCode.toTel = res.data.to;
                that.setData({
                    oldCode: oldCode,
                    oldtemplate: 'no_get'
                })
            }).catch(res => {
                console.log(res);
            })
    },
    // 新的手机号的验证
    account: function (e) {
        let code_disabled;
        let newCode = this.data.newCode;
        if (util.checkMobilePhone(e.detail.value)) {
            newCode.tel = e.detail.value;
            // 把输入的手机号存储
            this.setData({
                code_disabled: false,
                newCode: newCode
            })
        } else {
            this.setData({
                code_disabled: true
            })
        }
    },
    // 新的手机号再次发送验证
    sendNew: function (e) {
        let that = this;
        let newCode = that.data.newCode;
        util.PRL(API_URL.sendSms, { mobile: newCode.tel, type: 'bind' })
            .then(res => {
                if (res.data.status == 200) {
                    newCode.disabled = false;
                    key = res.data.data.key;
                    that.setData({
                        code_disabled: true,
                        newCode: newCode
                    })
                    countdown(that, 120);
                } else {
                    newCode.disabled = true;
                    that.setData({
                        code_disabled: false,
                        newCode: newCode
                    })
                    util.showNotice(res.data.msg)
                }
            }).catch(res => {
                console.log(res);
            })
    },
    // 再次发送验证
    sendNewCode: function (e) {
        console.log("click")
        let that = this;
        // 获取输入的code
        let code = e.detail.value.code;
        if (code.length == 0) {
            my.showToast({
                content: '请输入验证码',
                duration: 2000
            });
            return
        }
        var params = {
            account: that.data.newCode.tel,
            type: 'mobile',
            key: key,
            code: code
        }
        console.log(params)
        util.PRL(API_URL.bindAccount, params)
            .then(res => {
                console.log(res);
                if (res.data.status == 200) {
                    clearTimeout(time);
                    // 把状态置为初始
                    that.setData({
                        template: 'success',
                        code_time: '获取验证码',
                        code_disabled: true,
                    })
                    // 获取用户信息
                    let user = my.getStorageSync('user').data;
                    // 更新缓存值
                    user.phone = that.data.newCode.tel;
                    my.setStorage({
                        key: 'user',
                        data: user,
                    })
                }
            }).catch(res => {
                console.log(res);
            })
    },
    // 重新绑定成功之后跳转我的页面
    goToUser: function (e) {
        my.redirectTo({
            url: '/pages/user/user'
        })
    }
})